/*
 * InfoBox.java
 *
 * Created on den 7 oktober 2008, 21:14
 */
package bezier;

import bezier.curves.BezierCurve;
import bezier.curves.BSplineCurve;
import bezier.curves.Curve;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 *
 * @author  Gussoh
 */
public class InfoBox extends javax.swing.JDialog implements StateChangeListener, WindowListener {

    private BezierPanel bezierPanel;
    
    /** Creates new form InfoBox */
    public InfoBox(java.awt.Frame parent, BezierPanel bezierPanel) {
        super(parent);
        this.bezierPanel = bezierPanel;
        initComponents();
        getRootPane().setDefaultButton(closeButton);
        updateInfoLabel();
        bezierPanel.addChangeListener(this);
        addWindowListener(this);
    }

    private void updateInfoLabel() {
        TreeMap<Integer, Integer> bezierDegrees = new TreeMap<Integer, Integer>();
        for (Curve curve : bezierPanel.getCurves()) {
            if (curve instanceof BezierCurve) {
                int degree = curve.getControlPoints().size() - 1;
                if (bezierDegrees.containsKey(degree)) {
                    bezierDegrees.put(degree, bezierDegrees.get(degree).intValue() + 1);
                } else {
                    bezierDegrees.put(degree, 1);
                }
            }
        }

        TreeMap<Integer, Integer> bsplineDegrees = new TreeMap<Integer, Integer>();
        for (Curve curve : bezierPanel.getCurves()) {
            if (curve instanceof BSplineCurve) {
                int degree = ((BSplineCurve) curve).getDegree();
                if (bsplineDegrees.containsKey(degree)) {
                    bsplineDegrees.put(degree, bsplineDegrees.get(degree).intValue() + 1);
                } else {
                    bsplineDegrees.put(degree, 1);
                }
            }
        }
        

        StringBuilder sb = new StringBuilder();
        sb.append("<b>Bezier:</b>");
        sb.append("<table><tr><th>Degree</th><th>Number of</th></tr>");
        for (Entry<Integer, Integer> entry : bezierDegrees.entrySet()) {
            sb.append("<tr><td>").append(entry.getKey()).append("</td><td>").append(entry.getValue()).append("</td></tr>");
        }
        sb.append("</table>");

        sb.append("<b>B-Spline:</b>");
        sb.append("<table><tr><th>Degree</th><th>Number of</th></tr>");
        for (Entry<Integer, Integer> entry : bsplineDegrees.entrySet()) {
            sb.append("<tr><td>").append(entry.getKey()).append("</td><td>").append(entry.getValue()).append("</td></tr>");
        }
        sb.append("</table>");
        
        infoLabel.setText("<html>Number of curves: " + bezierPanel.getCurves().size() + "<br> <br>Number of curves of diffrent degrees:<br>" + sb.toString() + "</html>");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        infoLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bezier.BezierApp.class).getContext().getResourceMap(InfoBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("Form"); // NOI18N

        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        infoLabel.setText(resourceMap.getString("infoLabel.text")); // NOI18N
        infoLabel.setName("infoLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(infoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(infoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(closeButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
    setVisible(false);
    bezierPanel.removeChangeListener(this);
}//GEN-LAST:event_closeButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    public void stateChanged() {
        updateInfoLabel();
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        bezierPanel.removeChangeListener(this);
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
}
